﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutomationHost.ViewModels;
using CoreModels;

namespace AutomationHost.Areas.Case.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["HisoftSmartEntities"].ConnectionString;
            
        public ActionResult Index()
        {
            using (HisoftSmartEntities db = new HisoftSmartEntities(conn))
            {
                CaseIndexPage model = new CaseIndexPage();
                model.ProductTree = new Models.ProductTree() {Products = new List<Product>()};
                foreach (var p in db.Products.ToList())
                {
                    p.FeaturesModel = new List<Feature>();
                    foreach (var f in p.Features.ToList())
                    {
                        f.TestCasesModel = new List<TestCase>();
                        foreach (var tc in f.TestCases.ToList())
                        {
                            f.TestCasesModel.Add(tc);
                        }

                        p.FeaturesModel.Add(f);
                    }

                    model.ProductTree.Products.Add(p);                    
                }

                if (!string.IsNullOrEmpty(Request["caseid"]))
                {
                    int id = int.Parse(Request["caseid"]);
                    model.CurrentTestCase = db.TestCases.First(c => c.ID == id);
                    List<ActionInstance> instances = new List<ActionInstance>();
                    foreach (var step in model.CurrentTestCase.Steps.Split(';'))
                    {
                        if (string.IsNullOrEmpty(step)) continue;
                        int actionId = int.Parse(step.Split(',')[0]);
                        //List<string> param = step.Split(',').Skip(1).ToList();
                        Action action = db.Actions.Where(a => a.ID == actionId).First();
                        ActionInstance instance = new ActionInstance() 
                        {
                            Name = action.Name,
                            Params = step.Substring(actionId.ToString().Length + 1),
                            Description = action.Description
                        };

                        instances.Add(instance);
                    }

                    model.CurrentTestCase.ActionInstances = instances;
                }
                else if (!string.IsNullOrEmpty(Request["featureid"]))
                {
                    int id = int.Parse(Request["featureid"]);
                    model.CurrentFeature = db.Features.First(c => c.ID == id);
                }
                else if (!string.IsNullOrEmpty(Request["productid"]))
                {
                    int id = int.Parse(Request["productid"]);
                    model.CurrentProduct = db.Products.First(c => c.ID == id);
                }

                return View(model);
            }
        }
    }
}
